/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.evenmo.string;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * This is Description
 *
 * @author moyongjun
 * @date 2020/01/06
 */
public class Thread_parallelStreamAPI {
    public static void main(String[] args) {
        List<Integer> nums=new ArrayList<>();

        Random r=new Random();
        for (int i = 0; i < 10000; i++) {
            nums.add(100000+r.nextInt(100001));
        }



new ArrayList<>().iterator().next();

        long start=System.currentTimeMillis();
        nums.parallelStream().forEach(Thread_parallelStreamAPI::isPrime);
        long end=System.currentTimeMillis();
        System.out.println("使用并发流耗时"+(end-start)+"毫秒");

        start=System.currentTimeMillis();
        nums.forEach(num->isPrime(num));
        end=System.currentTimeMillis();

        System.out.println("普通方式耗时"+(end-start)+"毫秒");
    }


    public static boolean isPrime(int num){
        for (int i = 2; i <= num/2; i++) {
            if (num%i==0) return false;
        }
        return true;
    }



}
